package logic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.inputmethod.CorrectionInfo;

public class Question implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7820551561037246111L;
	private String id;
	private String text;
	private String idCorrectAnswer;
	private String idTest;
	private List<Answer> answers;

	/* Data base fields */
	public static final String KEY_ID = "id";
	public static final String KEY_TEST_ID = "test_id";
	public static final String KEY_TEXT = "text";
	public static final String KEY_CORRECT_ANSWER = "correct_answer";

	public Question(String id, String text, String correctAnswer, String idTest) {
		this.id = id;
		this.text = text;
		this.idCorrectAnswer = correctAnswer;
		this.idTest = idTest;
	}

	public Question(String id, String text, List<Answer> answers) {
		this.id = id;
		this.text = text;
		this.answers = answers;
	}

	public Question(String text, List<Answer> answers) {
		this.text = text;
		this.answers = answers;
	}

	public String getCorrectAnswerId() {
		return idCorrectAnswer;
	}

	public String getId() {
		return id;
	}

	public String getText() {
		return text;
	}

	public List<Answer> getAnswers() {
		return answers;
	}

	public void setAnswers(List<Answer> answers) {
		this.answers = answers;
	}

	public static List<Question> getQuestionsFromJSON(String json) {
		ArrayList<Question> Questions = new ArrayList<Question>();
		try {
			// JSONObject Object = new JSONObject(JSON);
			JSONArray jsonArray = new JSONArray(json);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String id = jsonObject.getString("id_pregunta");
				String text = jsonObject.getString("texto");
				String correctAnsw = jsonObject.getString("id_respuesta_ok");
				String idTest = jsonObject.getString("id_test");

				Questions.add(new Question(id, text, correctAnsw, idTest));
			}
			return Questions;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
}
